// pages/blog/blog.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    starCount: 0,
    forksCount: 0,
    visitTotal: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.updateData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.updateData();
  },

  /**
   * 更新页面数据
   */
  updateData: function () {
    let that = this;
    wx.showLoading({
      title: '数据加载中',
      mask: true,
    });

    let i = 0;
    animateData();

    function animateData() {
      if (i < 20) {
        setTimeout(() => {
          that.setData({
            starCount: i,
            forksCount: i,
            visitTotal: i
          });
          i++;
          animateData();
        }, 20);
      } else {
        that.setData({
          starCount: that.formatNumber(5),
          forksCount: that.formatNumber(1),
          visitTotal: that.formatNumber(28)
        });
        wx.hideLoading();
      }
    }
  },

  /**
   * 格式化数字为K或W单位
   * @param {number} num - 要格式化的数字
   * @return {string} - 格式化后的字符串
   */
  formatNumber: function (num) {
    if (num >= 10000) {
      return (num / 10000).toFixed(1) + 'W';
    } else if (num >= 1000) {
      return (num / 1000).toFixed(1) + 'K';
    }
    return num.toString();
  },

  /**
   * 复制链接到剪贴板
   * @param {Object} e - 事件对象
   */
  copyLink: function (e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.link,
      success: () => {
        wx.showToast({
          title: '链接已复制',
          duration: 1000,
        });
      }
    });
  },

  /**
   * 导航到外部页面
   */
  navigateToBlog: function () {
    wx.navigateTo({
      url: '../psblog/psblog.wxml',
    });
  },

  /**
   * 显示二维码
   */
  showQrcode: function () {
    const qrCodeUrl = 'https://s3.bmp.ovh/imgs/2022/05/10/4cc2a559125a50cc.jpg';
    wx.previewImage({
      current: qrCodeUrl,
      urls: [qrCodeUrl]
    });
  }

});
